package view.component.weapon
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import enums.main.UISize;
	
	import feathers.controls.Label;
	import feathers.controls.List;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.controls.Scroller;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.controls.renderers.IListItemRenderer;
	import feathers.data.ListCollection;
	import feathers.layout.VerticalLayout;
	
	import model.vo.ItemVo;
	import model.vo.weapon.BaseWeaponVo;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.events.Event;
	
	import util.ItemUtil;
	import util.WeaponUtil;
	
	import view.event.ItemEvent;
	
	
	/**
	 * 武器详细情报基类
	 * @author £iang
	 * 
	 */	
	public class BaseWeaponDetailView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 武器名信息 */
		public var nameLabel:Label;
		/** 攻击力标题 */
		public var attackTitle:Label;
		/** 攻击力&加防御信息 */
		public var attackLabel:Label;
		/** 洞数标题 */
		public var slotTitle:Label;
		/** 洞数信息 */
		public var slotLabel:Label;
		/** 会心标题 */
		public var criticalTitle:Label;
		/** 会心信息 */
		public var criticalLabel:Label;
		/** 生产标题 */
		public var productTitle:Label;
		/** 生产价格信息 */
		public var productPriceLabel:Label;
		/** 强化标题 */
		public var strengthTitle:Label;
		/** 强化信息 */
		public var strengthPriceLabel:Label;
		/** 鉴定标题 */
		public var identifyTitle:Label;
		/** 鉴定信息 */
		public var identifyLabel:Label;
		/** 滚动条容器 */
		public var mainSC:ScrollContainer;
		/** 生产素材列表 */
		public var produceItemList:List;
		/** 强化素材列表 */
		public var strengthItemList:List;
		/** 鉴定素材列表 */
		public var identifyItemList:List;
		/** 示意图标题 */
		public var imageTitle:Label;
		/** “暂无”标签 */
		public var imageLabel:Label;
		/** 示意图 */
		public var image:Image;
		/** 武器种类 */
		public var weaponType:String;
		/** 插入项造成的偏移 */
		public var insertOffset:Number;
		/** 图片Y坐标 */
		public var imageY:Number;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** produceItemList数据 */
		protected var _listDataProduce:ListCollection;
		/** strengthItemList数据 */
		protected var _listDataStrength:ListCollection;
		/** identifyItemList数据 */
		protected var _listDataIdentify:ListCollection;
		/** 图片加载器 */
		protected var _imageLoader:Loader = new Loader();
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			mainSC = new ScrollContainer();
			mainSC.width = UISize.MAIN_W;
			mainSC.height = UISize.MAIN_H - 
				UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2;
			mainSC.hasElasticEdges = false;
			mainSC.horizontalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			this.addChild( mainSC );
			
			nameLabel = new Label();
			nameLabel.textRendererProperties.isHTML = true;
			mainSC.addChild( nameLabel );
			
			attackTitle = new Label();
			attackTitle.text = "攻击力：";
			mainSC.addChild( attackTitle );
			
			attackLabel = new Label();
			attackLabel.textRendererProperties.isHTML = true;
			mainSC.addChild( attackLabel );
			
			criticalTitle = new Label();
			criticalTitle.text = "会心率：";
			mainSC.addChild( criticalTitle );
			
			criticalLabel = new Label();
			mainSC.addChild( criticalLabel );
			
			slotTitle = new Label();
			slotTitle.text = "武器洞：";
			mainSC.addChild( slotTitle );
			
			slotLabel = new Label();
			mainSC.addChild( slotLabel );
			
			productTitle = new Label();
			productTitle.text = "   生产：";
			mainSC.addChild( productTitle );
			
			productPriceLabel = new Label();
			mainSC.addChild( productPriceLabel );
			
			produceItemList = new List();
			mainSC.addChild( produceItemList );
			produceItemList.width = 610 * this.dpiScale;
			produceItemList.itemRendererProperties.labelField  = "label";
			produceItemList.itemRendererProperties.iconField = "icon";
			produceItemList.itemRendererFactory = itemProduceRenderFactroy;
			produceItemList.hasElasticEdges = false;
			produceItemList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			produceItemList.verticalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			produceItemList.horizontalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			produceItemList.height = ( UISize.ITEM_LIST_ITEM_H * 4 + 10 * 3 ) * this.dpiScale;
			( produceItemList.layout as VerticalLayout ).gap = 10 * this.dpiScale;
			
			strengthTitle = new Label();
			strengthTitle.text = "   强化：";
			mainSC.addChild( strengthTitle );
			
			strengthPriceLabel = new Label();
			strengthPriceLabel.textRendererProperties.isHTML = true;
			mainSC.addChild( strengthPriceLabel );
			
			strengthItemList = new List();
			mainSC.addChild( strengthItemList );
			strengthItemList.width = 610 * this.dpiScale;
			strengthItemList.itemRendererProperties.labelField  = "label";
			strengthItemList.itemRendererProperties.iconField = "icon";
			strengthItemList.itemRendererFactory = itemStrengthRenderFactroy;
			strengthItemList.hasElasticEdges = false;
			strengthItemList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			strengthItemList.verticalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			strengthItemList.horizontalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			strengthItemList.height = ( UISize.ITEM_LIST_ITEM_H * 4 + 10 * 3 ) * this.dpiScale;
			( strengthItemList.layout as VerticalLayout ).gap = 10 * this.dpiScale;
			
			identifyTitle = new Label();
			identifyTitle.text = "   鉴定：";
			mainSC.addChild( identifyTitle );
			
			identifyLabel = new Label();
			mainSC.addChild( identifyLabel );
			
			identifyItemList = new List();
			mainSC.addChild( identifyItemList );
			identifyItemList.width = 610 * this.dpiScale;
			identifyItemList.itemRendererProperties.labelField  = "label";
			identifyItemList.itemRendererProperties.iconField = "icon";
			identifyItemList.itemRendererFactory = itemIdentifyRenderFactroy;
			identifyItemList.hasElasticEdges = false;
			identifyItemList.scrollerProperties.scrollBarDisplayMode = Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			identifyItemList.verticalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			identifyItemList.horizontalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			identifyItemList.height = ( UISize.ITEM_LIST_ITEM_H * 2 + 10 ) * this.dpiScale;
			( identifyItemList.layout as VerticalLayout ).gap = 10 * this.dpiScale;
			
			imageTitle = new Label();
			imageTitle.text = "示意图：";
			mainSC.addChild( imageTitle );
			
			imageLabel = new Label();
			mainSC.addChild( imageLabel );
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			nameLabel.x = 20 * this.dpiScale;
			
			attackTitle.x = 20 * this.dpiScale;
			attackTitle.y = 60 * this.dpiScale;
			attackLabel.x = 150 * this.dpiScale;
			attackLabel.y = 60 * this.dpiScale;
		}
		
		
		/**
		 * produceItem皮肤 
		 * @return 
		 * 
		 */		
		protected function itemProduceRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = ItemUtil.commonItem();
			renderer.addEventListener( starling.events.Event.TRIGGERED, itemProduceTriggeredHandler );
			return renderer;
		}
		
		/**
		 * strengthItem皮肤 
		 * @return 
		 * 
		 */		
		protected function itemStrengthRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = ItemUtil.commonItem();
			renderer.addEventListener( starling.events.Event.TRIGGERED, itemStrengthTriggeredHandler );
			return renderer;
		}
		
		/**
		 * identifyItem皮肤 
		 * @return 
		 * 
		 */		
		protected function itemIdentifyRenderFactroy():IListItemRenderer
		{
			var renderer:DefaultListItemRenderer = ItemUtil.commonItem();
			renderer.addEventListener( starling.events.Event.TRIGGERED, itemIdentifyTriggeredHandler );
			return renderer;
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */		
		public function playHideAnimation():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0);
			Starling.juggler.add( tween );
		}
		
		/**
		 * 设置详细数据 
		 * @param weaponVo 武器数据
		 * 
		 */		
		public function setData( weaponVo:BaseWeaponVo ):void
		{
			mainSC.verticalScrollPosition = 0;
			
			nameLabel.text = '<font color="#' + 
				WeaponUtil.getRareColor( weaponVo.rare ).toString( 16 ) + 
				'">' + weaponVo.name + '(' + weaponVo.nickName + ')' + '</font>';
			attackLabel.text = weaponVo.attack.toString();
			
			if ( weaponVo.defAdd != "" && weaponVo.defAdd )
			{
				attackLabel.text += " (" + weaponVo.defAdd + ")";
			}
			
			criticalLabel.text = weaponVo.critical + "%";
			slotLabel.text = WeaponUtil.getSlotText( weaponVo.slot );
			
			var len:int;
			var i:int;
			var num:int;
			
			disposeProduceItems();
			_listDataProduce = new ListCollection([]);
			
			if ( weaponVo.producePrice == -1 )
			{
				productPriceLabel.text = "无";
			}
			else
			{
				productPriceLabel.text = weaponVo.producePrice + "z";
				len = weaponVo.produceItem.length;
				
				for ( i = 0; i < len; i ++ )
				{
					num = weaponVo.produceNum.length > 0 ? weaponVo.produceNum[ i ] : 1;
					_listDataProduce.push( ItemUtil.mapItemVo2ListObj( weaponVo.produceItem[ i ] as ItemVo, num ) );
				}
			}
			
			produceItemList.dataProvider = _listDataProduce;
			disposeStrengthItems();
			_listDataStrength = new ListCollection([]);
			
			if ( weaponVo.strengthPrice == -1 )
			{
				strengthPriceLabel.text = "无";
			}
			else
			{
				strengthPriceLabel.text = weaponVo.strengthPrice + "z";
				len = weaponVo.strengthItem.length;
				
				for ( i = 0; i < len; i ++ )
				{
					num = weaponVo.strengthNum.length > 0 ? weaponVo.strengthNum[ i ] : 1;
					_listDataStrength.push( ItemUtil.mapItemVo2ListObj( weaponVo.strengthItem[ i ] as ItemVo, num ) );
				}
			}
			
			strengthItemList.dataProvider = _listDataStrength;
			disposeIdentifyItems();
			_listDataIdentify = new ListCollection([]);
			
			if ( weaponVo.identifyItem.length == 0 )
			{
				identifyLabel.text = "无";
			}
			else
			{
				identifyLabel.text = "";
				len = weaponVo.identifyItem.length;
				
				for ( i = 0; i < len; i ++ )
				{
					num = weaponVo.identifyNum.length > 0 ? weaponVo.identifyNum[ i ] : 1;
					_listDataIdentify.push( ItemUtil.mapItemVo2ListObj( weaponVo.identifyItem[ i ] as ItemVo, num ) );
				}
			}
			
			identifyItemList.dataProvider = _listDataIdentify;
			
			var urlRequest:URLRequest = new URLRequest(
				"/data/pic/weapon/" + weaponType + "/" + weaponVo.picId + ".jpg"
			);
			
			disposeWeaponImage();
			
			if ( weaponVo.picId == -1 )
			{
				imageLabel.text = "暂无";
			}
			else
			{
				imageLabel.text = "";
				_imageLoader.load( urlRequest );
				_imageLoader.contentLoaderInfo.addEventListener( flash.events.Event.COMPLETE, completeHandler );
			}
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理生产素材item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemProduceTriggeredHandler( event:starling.events.Event ):void
		{
			var itemId:int =  _listDataProduce.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			this.dispatchCommonEvent( itemEvent );
		}
		
		/**
		 * 处理强化素材item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemStrengthTriggeredHandler( event:starling.events.Event ):void
		{
			var itemId:int =  _listDataStrength.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			this.dispatchCommonEvent( itemEvent );
		}
		
		/**
		 * 处理鉴定素材item被点击后派发的事件 
		 * @param event 道具item被点击后派发的事件 
		 * 
		 */		
		protected function itemIdentifyTriggeredHandler( event:starling.events.Event ):void
		{
			var itemId:int =  _listDataIdentify.data[ ( event.target as DefaultListItemRenderer ).index ].itemId;	
			var itemEvent:ItemEvent = new ItemEvent( ItemEvent.SEARCH_ITEM );
			
			itemEvent.curSearchItemId = itemId;
			this.dispatchCommonEvent( itemEvent );
		}
		
		/**
		 * 图片加载完毕 
		 * @param event
		 * 
		 */		
		protected function completeHandler( event:flash.events.Event ):void
		{
			_imageLoader.contentLoaderInfo.removeEventListener( flash.events.Event.COMPLETE, completeHandler );
			
			var bitmap:Bitmap = event.currentTarget.content as Bitmap;
			
			image = Image.fromBitmap( bitmap );
			mainSC.addChild( image );
			image.x = 150 * this.dpiScale;
			image.y = ( imageY + insertOffset ) * this.dpiScale;
			image.scaleX = image.scaleY = Math.max( this.dpiScale, 0.8 );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( this );
			nameLabel.dispose(); nameLabel = null;
			attackTitle.dispose(); attackTitle = null;
			attackLabel.dispose(); attackLabel = null;
			slotTitle.dispose(); slotTitle = null;
			slotLabel.dispose(); slotLabel = null;
			criticalTitle.dispose(); criticalTitle = null;
			criticalLabel.dispose(); criticalLabel = null;
			productTitle.dispose(); productTitle = null;
			productPriceLabel.dispose(); productPriceLabel = null;
			strengthTitle.dispose(); strengthTitle = null;
			strengthPriceLabel.dispose(); strengthPriceLabel = null;
			identifyTitle.dispose(); identifyTitle = null;
			identifyLabel.dispose(); identifyLabel = null;
			imageTitle.dispose(); imageTitle = null;
			imageLabel.dispose(); imageLabel = null;
			/*mainSC.dispose(); mainSC = null;*/
			disposeIdentifyItems( true );
			disposeStrengthItems( true );
			disposeProduceItems( true );
			produceItemList.dispose();
			produceItemList = null;
			strengthItemList.dispose();
			strengthItemList = null;
			identifyItemList.dispose();
			identifyItemList = null;
			disposeWeaponImage();
			_imageLoader.contentLoaderInfo.removeEventListener( flash.events.Event.COMPLETE, completeHandler );
			_imageLoader = null;
			super.dispose();
		}
		
		/**
		 * 析构生产素材列表中的item 
		 * 
		 */		
		public function disposeProduceItems( isDeep:Boolean = false ):void
		{
			var i:int;
			var len:int;
			
			if ( _listDataProduce )
			{
				len = _listDataProduce.length;
				
				for ( i = 0; i < len; i ++ )
				{
					( _listDataProduce.data[ i ].icon as Image ).dispose();
					( _listDataProduce.data[ i ].icon as Image ).texture.dispose();
					_listDataProduce.data[ i ].icon = null; 
					_listDataProduce.data[ i ] = null;
				}
				
				_listDataProduce.removeAll();
				_listDataProduce = null;
			}
		}
		
		/**
		 * 析构强化素材列表中的item 
		 * 
		 */		
		public function disposeStrengthItems( isDeep:Boolean = false ):void
		{
			var i:int;
			var len:int;
		
			if ( _listDataStrength )
			{
				len = _listDataStrength.length;
				
				for ( i = 0; i < len; i ++ )
				{
					( _listDataStrength.data[ i ].icon as Image ).dispose();
					( _listDataStrength.data[ i ].icon as Image ).texture.dispose();
					_listDataStrength.data[ i ].icon = null; 
					_listDataStrength.data[ i ] = null;
				}
				
				_listDataStrength.removeAll();
				_listDataStrength = null;
			}
		}
		
		/**
		 * 析构鉴定素材列表中的item 
		 * 
		 */		
		public function disposeIdentifyItems( isDeep:Boolean = false ):void
		{
			var i:int;
			var len:int;
			
			if ( _listDataIdentify )
			{
				len = _listDataIdentify.length;
				
				for ( i = 0; i < len; i ++ )
				{
					( _listDataIdentify.data[ i ].icon as Image ).dispose();
					( _listDataIdentify.data[ i ].icon as Image ).texture.dispose();
					_listDataIdentify.data[ i ].icon = null; 
					_listDataIdentify.data[ i ] = null;
				}
				
				_listDataIdentify.removeAll();
				_listDataIdentify = null;
			}
		}
		
		public function disposeWeaponImage():void
		{
			if ( image )
			{
				mainSC.removeChild( image );
				image.dispose();
				image.texture.dispose();
				image = null;
			}
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseWeaponDetailView()
		{
			super();
		}
	}
}